package com.wison.quartz.job;

import com.wison.bean.GS;
import com.wison.bean.GSD;
import org.nutz.dao.Dao;
import org.nutz.http.Http;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/3.
 */
@IocBean
public class GSJob implements Job {

    @Inject
    private Dao dao;

    @Inject
    private DataSource dataSource;

    @Inject("java:$conf.get('server_url')")
    private String wsdlurl;

    @Inject("java:$conf.get('dwID')")
    private String dwID;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        try {


            Map<String, Object> parms = new HashMap<String, Object>();
            parms.put("dw", dwID);
            String response = Http.post(wsdlurl + "/getGS.do", parms, 50 * 1000);
            GS gs = Json.fromJson(GS.class, response);


            PreparedStatement pstmt = dataSource.getConnection().prepareStatement("{call ClientPro_Card_loss(?,?,'',1,'127.0.0.1','admin','')}");
            for (GSD gsd : gs.getList()) {
                parms.put("id", gsd.getId());

                pstmt.setInt(1, Integer.parseInt(gsd.getUser_serial()));
                pstmt.setString(2, gsd.getKh());
                pstmt.execute();

                response = Http.post(wsdlurl + "/updateGS.do", parms, 50 * 1000);
                System.out.println(response);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
